import { cloneDeep } from "lodash";
import { SearchDto } from "./SearchDto";

// 表格配置项
const TableOptions = {
  loading: true, // 表格数据是否加载中
  data: [], // 表格呈现的数据
  currentPage: 1, // 表格数据当前呈现页
  pageSize: 20, // 表格数据当前页显示条数

  searchDto: new SearchDto(),
  rowKey: "_X_ROW_KEY", // 默认值参照于 vxe-table
  pageSizes: [20, 100, 500, 1000], //
  total: 0, // 总数据
  get totalPage() {
    return Math.ceil(this.total / this.pageSize);
  },
};
/**
 * 创建一个表格配置项，可部分配置覆盖
 * @param options
 * @returns
 */
export function createTableOptions(options: Partial<typeof TableOptions> = {}): Partial<typeof TableOptions> {
  if (!options) {
    options = {};
  }
  return {
    ...cloneDeep(TableOptions),
    ...options,
  };
}

const FilterData = {
  searchKey: "",
};
export function createFilterData<T>(data: Partial<T & typeof FilterData> = {}) {
  if (!data) {
    data = {};
  }
  return { ...cloneDeep(FilterData), ...data };
}

const ListOptions = {
  loading: false,
  data: [],
  currentPage: 1, // 表格数据当前呈现页
  pageSize: 20, // 表格数据当前页显示条数
  hasMore: true,

  /**
   * 重置参数
   */
  reset() {
    this.loading = false;
    this.data = [];
    this.currentPage = 1;
    this.hasMore = true;
  },
};
export function createListOptions(options: Partial<typeof ListOptions> = {}): Partial<typeof ListOptions> {
  if (!options) {
    options = {};
  }
  return {
    ...cloneDeep(ListOptions),
    ...options,
  };
}
